package com.zhubjiept.app.common.widget;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhubjiept.app.R;
import com.zhubjiept.app.jinrirong.config.ApiFactory;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by wangsy on 2019/1/20.
 */

public class TeamMemberAdapter extends BaseAdapter {
    List<TeamMember> members;
    Context context;

    public TeamMemberAdapter(Context context, List<TeamMember> members) {
        this.context = context;
        this.members = members;
    }

    @Override
    public int getCount() {
        return members.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        TeamMember member = members.get(position);
        View v;
        final ViewHolder holder;

        //滑入ListView中的item view没有缓存
        String img = String.valueOf(member.getHeadImg());
        if (convertView == null) {
            v = LayoutInflater.from(context).inflate(R.layout.item_my_team, null);//动态加载滑入ListView的item的子布局文件，并绘制item view
            holder = new ViewHolder();
        /*  缓存子布局文件中的控件对象*/
            holder.uid = v.findViewById(R.id.member_id);
            holder.regTime = v.findViewById(R.id.tv_reg_time);
            holder.headerImage = v.findViewById(R.id.headerImage);
            holder.mobile = v.findViewById(R.id.tv_mobile);
            holder.memberLevel = v.findViewById(R.id.tv_member_level);

            holder.uid.setText(String.valueOf(member.getId()));
            holder.mobile.setText(String.valueOf(member.getMobile()));
            holder.regTime.setText(new SimpleDateFormat("yyyy-MM-dd").format(member.getRegTime()));
            holder.memberLevel.setText(member.getMtype());
            if (!("".equals(img) || "null".equals(img))) {
                Glide.with(convertView.getContext()).load(ApiFactory.RESOURCE_URL + img).into(holder.headerImage);
            } else {
                holder.headerImage.setImageResource(R.drawable.ic_user);
            }
            v.setTag(holder);
        } else {
            v = convertView;                            //取出缓存的item View对象
            holder = (ViewHolder) v.getTag();                            //重新获取ViewHolder对象
        }

        holder.uid.setText(String.valueOf(member.getId()));
        holder.mobile.setText(String.valueOf(member.getMobile()));
        holder.regTime.setText(new SimpleDateFormat("yyyy-MM-dd").format(member.getRegTime()));
        holder.memberLevel.setText(member.getMtype());
        if (!("".equals(img) || "null".equals(img))) {
            Glide.with(convertView.getContext()).load(ApiFactory.RESOURCE_URL + img).into(holder.headerImage);
        } else {
            holder.headerImage.setImageResource(R.drawable.ic_user);
        }
        return v;
    }


    /*  定义ViewHolder用来缓存item view控件  */
    class ViewHolder {
        ImageView headerImage;
        TextView memberName;
        TextView memberLevel;
        TextView mobile;
        TextView uid;
        TextView regTime;
    }

}
